package com.longcai.service.impl.nearbyshop;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.longcai.bean.ApiResult;
import com.longcai.bean.PageHelper;
import com.longcai.entity.auth.ClyjShopsArea;
import com.longcai.entity.nearby.ClyjNearbyGoodsType;
import com.longcai.entity.nearby.ClyjNearbyShops;
import com.longcai.mapper.ClyjNearbyShopsMapper;
import com.longcai.mapper.clyjuser.ClyjShopsAreaMapper;
import com.longcai.mapper.nearbyshop.ClyjNearbyGoodsTypeMapper;
import com.longcai.page.table.PageTableResponse;
import com.longcai.service.nearbyshop.ClyjNearbyGoodsTypeService;
import com.longcai.util.LoginUser;
import com.longcai.util.UserUtil;
import com.longcai.util.listUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 附近商品类型 服务实现类
 * </p>
 *
 * @author M
 * @since 2019-04-12
 */
@Service
public class ClyjNearbyGoodsTypeServiceImpl extends ServiceImpl<ClyjNearbyGoodsTypeMapper, ClyjNearbyGoodsType> implements ClyjNearbyGoodsTypeService {

    private Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    private ClyjNearbyShopsMapper clyjNearbyShopsMapper;
    @Autowired
    private ClyjNearbyGoodsTypeMapper clyjNearbyGoodsTypeMapper;
    @Autowired
    private ClyjShopsAreaMapper clyjShopsAreaMapper;

    /**
     * 获取商品类型
     *
     * @param clyjNearbyGoodsType
     * @param pageHelper
     * @return
     */
    @Override
    public PageTableResponse typeList(ClyjNearbyGoodsType clyjNearbyGoodsType, PageHelper pageHelper, String id) {
        //查询登录用户角色
        LoginUser loginUser = UserUtil.getLoginUser();
        QueryWrapper<ClyjShopsArea> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", loginUser.getUserId());
        List<ClyjShopsArea> clyjShopsArea = this.clyjShopsAreaMapper.selectList(queryWrapper);
        //分页
        Page<ClyjNearbyGoodsType> page = new Page<ClyjNearbyGoodsType>(pageHelper.getStart(), pageHelper.getLength());
        QueryWrapper<ClyjNearbyGoodsType> queryWrapper2 = new QueryWrapper<>();
        QueryWrapper<ClyjNearbyShops> queryWrapper3 = new QueryWrapper<>();
        if (!StringUtils.isEmpty(clyjNearbyGoodsType.getName())) {
            queryWrapper2.like("name", clyjNearbyGoodsType.getName());
        }
        queryWrapper2.eq("shop_id", id);
      /*  if (!StringUtils.isEmpty(clyjNearbyGoodsType.getShopId())) {
            queryWrapper.eq("shop_id", clyjNearbyGoodsType.getShopId());
        }*/
        //判断是否为代理
        List list = new ArrayList();
        List list1 = new ArrayList();
        if (clyjShopsArea.size() > 0) {
            for (ClyjShopsArea clyjShopsArea1 : clyjShopsArea) {
                list.add(clyjShopsArea1.getAddressName());
            }
            //根据代理地区查询店铺
            String s = listUtil.listToString(list);
            queryWrapper3.like("shops_address", s);
            List<ClyjNearbyShops> clyjNearbyShopsList = this.clyjNearbyShopsMapper.selectList(queryWrapper3);
            //遍历查询店铺id
            for (ClyjNearbyShops clyjNearbyShops : clyjNearbyShopsList) {
                list1.add(clyjNearbyShops.getShopsId());
            }
            //加条件
            queryWrapper2.in("shop_id", list1);
        }
        if (clyjShopsArea.size() <= 0) {
            queryWrapper3.eq("mich_id", loginUser.getUserId());
            List<ClyjNearbyShops> clyjNearbyShopsList = this.clyjNearbyShopsMapper.selectList(queryWrapper3);
            //遍历查询店铺id
            for (ClyjNearbyShops clyjNearbyShops : clyjNearbyShopsList) {
                list1.add(clyjNearbyShops.getShopsId());
            }
            //加条件
            queryWrapper2.in("shop_id", list1);
        }

        IPage<ClyjNearbyGoodsType> goodsTypeIPage = this.clyjNearbyGoodsTypeMapper.selectPage(page, queryWrapper2);
        List<ClyjNearbyGoodsType> records = goodsTypeIPage.getRecords();
        for (ClyjNearbyGoodsType clyjNearbyGoodsType1 : records) {
            //查询店铺名
            QueryWrapper<ClyjNearbyShops> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("shops_id", clyjNearbyGoodsType1.getShopId());
            ClyjNearbyShops clyjNearbyShops = this.clyjNearbyShopsMapper.selectOne(queryWrapper1);
            clyjNearbyGoodsType1.setShopId(clyjNearbyShops.getShopsName());
        }
        goodsTypeIPage.setRecords(records);
        PageTableResponse pageTableResponse = new PageTableResponse(goodsTypeIPage.getTotal(),
                goodsTypeIPage.getTotal(), goodsTypeIPage.getRecords());
        return pageTableResponse;
    }

    /**
     * 获取商品类型
     *
     * @param clyjNearbyGoodsType
     * @param pageHelper
     * @return
     */
    @Override
    public PageTableResponse typeLists(ClyjNearbyGoodsType clyjNearbyGoodsType, PageHelper pageHelper) {
        //查询登录用户角色
        LoginUser loginUser = UserUtil.getLoginUser();
        QueryWrapper<ClyjShopsArea> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", loginUser.getUserId());
        List<ClyjShopsArea> clyjShopsArea = this.clyjShopsAreaMapper.selectList(queryWrapper);
        //分页
        Page<ClyjNearbyGoodsType> page = new Page<ClyjNearbyGoodsType>(pageHelper.getStart(), pageHelper.getLength());
        QueryWrapper<ClyjNearbyGoodsType> queryWrapper2 = new QueryWrapper<>();
        QueryWrapper<ClyjNearbyShops> queryWrapper3 = new QueryWrapper<>();
        if (!StringUtils.isEmpty(clyjNearbyGoodsType.getName())) {
            queryWrapper2.like("name", clyjNearbyGoodsType.getName());
        }
        //判断是否为代理
        List list = new ArrayList();
        List list1 = new ArrayList();
        if (clyjShopsArea.size() > 0) {
            for (ClyjShopsArea clyjShopsArea1 : clyjShopsArea) {
                list.add(clyjShopsArea1.getAddressName());
            }
            //根据代理地区查询店铺
            String s = listUtil.listToString(list);
            queryWrapper3.like("shops_address", s);
            List<ClyjNearbyShops> clyjNearbyShopsList = this.clyjNearbyShopsMapper.selectList(queryWrapper3);
            //遍历查询店铺id
            for (ClyjNearbyShops clyjNearbyShops : clyjNearbyShopsList) {
                list1.add(clyjNearbyShops.getShopsId());
            }
            //加条件
            queryWrapper2.in("shop_id", list1);
        }
        if (clyjShopsArea.size() <= 0) {
            queryWrapper3.eq("mich_id", loginUser.getUserId());
            List<ClyjNearbyShops> clyjNearbyShopsList = this.clyjNearbyShopsMapper.selectList(queryWrapper3);
            //遍历查询店铺id
            for (ClyjNearbyShops clyjNearbyShops : clyjNearbyShopsList) {
                list1.add(clyjNearbyShops.getShopsId());
            }
            //加条件
            queryWrapper2.in("shop_id", list1);
        }

        IPage<ClyjNearbyGoodsType> goodsTypeIPage = this.clyjNearbyGoodsTypeMapper.selectPage(page, queryWrapper2);
        List<ClyjNearbyGoodsType> records = goodsTypeIPage.getRecords();
        for (ClyjNearbyGoodsType clyjNearbyGoodsType1 : records) {
            //查询店铺名
            QueryWrapper<ClyjNearbyShops> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("shops_id", clyjNearbyGoodsType1.getShopId());
            ClyjNearbyShops clyjNearbyShops = this.clyjNearbyShopsMapper.selectOne(queryWrapper1);
            clyjNearbyGoodsType1.setShopId(clyjNearbyShops.getShopsName());
        }
        goodsTypeIPage.setRecords(records);
        PageTableResponse pageTableResponse = new PageTableResponse(goodsTypeIPage.getTotal(),
                goodsTypeIPage.getTotal(), goodsTypeIPage.getRecords());
        return pageTableResponse;
    }

    /**
     * 根据名称查询商品类型
     *
     * @param name
     * @return
     */
    @Override
    public ClyjNearbyGoodsType getName(String name) {
        try {
            QueryWrapper<ClyjNearbyGoodsType> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("name", name);
            ClyjNearbyGoodsType clyjNearbyGoodsType = this.clyjNearbyGoodsTypeMapper.selectOne(queryWrapper);
            logger.debug("查询{}", clyjNearbyGoodsType);
            return clyjNearbyGoodsType;
        } catch (Exception e) {
            logger.error("查询失败");
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 新增商品类型
     *
     * @param clyjNearbyGoodsType
     * @return
     */
    @Override
    public ClyjNearbyGoodsType saveType(ClyjNearbyGoodsType clyjNearbyGoodsType) {
        try {
            ClyjNearbyGoodsType clyjNearbyGoodsType1 = clyjNearbyGoodsType;
            clyjNearbyGoodsType1.setId(UUID.randomUUID().toString().replace("-", ""));
            clyjNearbyGoodsType1.setCreateTime(new Date());
            Integer integer = this.clyjNearbyGoodsTypeMapper.insert(clyjNearbyGoodsType1);
            logger.debug("新增成功++++++++++++", integer);
            return clyjNearbyGoodsType1;
        } catch (Exception e) {
            logger.error("新增失败");
            e.printStackTrace();
            e.getMessage();
        }

        return null;
    }

    /**
     * 根据id获取商品类型
     *
     * @param id
     * @return
     */
    @Override
    public ApiResult getById(String id) {

        try {
            ClyjNearbyGoodsType clyjNearbyGoodsType = this.clyjNearbyGoodsTypeMapper.selectById(id);
            logger.debug("查询成功++++++++++++++", clyjNearbyGoodsType);
            return ApiResult.success(clyjNearbyGoodsType);
        } catch (Exception e) {
            logger.error("查询失败");
            e.printStackTrace();
            return ApiResult.fail("查询失败" + e.getMessage());
        }
    }

    /**
     * 修改商品类型
     *
     * @param clyjNearbyGoodsType
     * @return
     */
    @Override
    public Integer updateType(ClyjNearbyGoodsType clyjNearbyGoodsType) {

        try {
            clyjNearbyGoodsType.setUpdateTime(new Date());
            return this.clyjNearbyGoodsTypeMapper.updateById(clyjNearbyGoodsType);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 删除商品类型
     *
     * @param id
     */
    @Override
    public void deleteById(String id) {
        try {
            QueryWrapper<ClyjNearbyGoodsType> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("id", id);
            Integer integer = this.clyjNearbyGoodsTypeMapper.delete(queryWrapper);
        } catch (Exception e) {
            e.printStackTrace();
            e.getMessage();
        }
    }

    /**
     * 获取所有商品类型
     *
     * @return
     */
    @Override
    public List<ClyjNearbyGoodsType> getType() {
        return clyjNearbyGoodsTypeMapper.getType();
    }
}
